/*
 * lexerstring.c
 *
 *  Created on: 2013-5-8
 *      Author: xkaying
 */

#include "lexer.h"
#include "lexerpackage.h"
#include "../typedef/token.h"

#include <assert.h>
#include "lexerstring.h"

char *LexerString(char *ch, int *line, int *len, Lexer *plex, int size)
{
	assert(ch);
	assert(line);
	assert(len);
	assert(plex);
	int state;
	state = 0;
	Token *ptoken;
	ptoken = 0;
	while ((*len) < size) {
		switch (state)
		{
			case 0: {
				//" '
				switch (*ch)
				{
					//字符和字符串
					case '"': {
						state = 1;
						(*len)++;
						ch++;
						ptoken = TokenInit(TokenType_String);
						ptoken->str = ch;
						continue;
					}
						//注释多行
					case '\'': {
						//继续读取字符,检查是否为package
						state = 2;
						(*len)++;
						ch++;
						ptoken = TokenInit(TokenType_Char);
						ptoken->str = ch;
						continue;
					}
					default: {
						return ch;
					}
				}
				printf("错误的lexer comment状态机.状态:0\n");
				return 0;
			}
			case 1: {
				switch (*ch)
				{
					//注释单行
					case '"': {
						//结束
						ptoken->name = SymbolTablePush(ptoken->str,
								ptoken->len);
						LexerPush(plex, ptoken);
						(*len)++;
						ch++;
						return ch;
					}
					case '\\': {
						state = 10;
						(*len)++;
						ch++;
						ptoken->len++;
						continue;
					}
					default: {
						(*len)++;
						ch++;
						ptoken->len++;
						continue;
					}
				}
				printf("错误的lexer string状态机.状态:1\n");
				return 0;
			}
			case 10: {
				//转义一个字符
				(*len)++;
				ch++;
				ptoken->len++;
				state = 1;
				continue;
			}
			case 2: {
				switch (*ch)
				{
					//注释单行
					case '\'': {
						//结束
						ptoken->name = SymbolTablePush(ptoken->str,
								ptoken->len);
						LexerPush(plex, ptoken);
						(*len)++;
						ch++;
						return ch;
					}
					case '\\': {
						state = 20;
						(*len)++;
						ch++;
						ptoken->len++;
						continue;
					}
					default: {
						(*len)++;
						ch++;
						ptoken->len++;
						continue;
					}
				}
				printf("错误的lexer string状态机.状态:2\n");
				return 0;
			}
			case 20: {
				//转义一个字符
								(*len)++;
								ch++;
								ptoken->len++;
								state = 2;
								continue;
			}
			default:{
				printf("错误的lexer string状态机.进入未知状态.\n");
				return 0;
			}

		}

	}
	if(ptoken==0){
		return ch;
	}
	if(ptoken->type==TokenType_String){
		printf("字符串没有进行结尾.\n");
	}else{
		printf("字符没有进行结尾.\n");
	}

	return 0;
}
